package mi.payment.ui.admin;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.settings.SecurityPasswordSettingsOperation;
import mi.payment.ui.base.ParentActivity;
import sunmi.sunmiui.title.Title;

/**
 * 修改安全密码
 * Created by sunmi on 2017/2/16.
 */

public class UpdateSecurityPasswordActivity extends ParentActivity {

    private EditText editOldPwd;
    private EditText editNewPwdOne;
    private EditText editNewPwdTwo;
    private int oldPwdLength;
    private int newPwdLengthOne;
    private int newPwdLengthTwo;
    private RelativeLayout relCommit;
    private SecurityPasswordSettingsOperation mSecurityPasswordSettingsOperation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_password);
        mSecurityPasswordSettingsOperation = SecurityPasswordSettingsOperation.getInstance();
        initView();
    }

    private void initView() {
        TextView tvNum;
        TextView txt_head_title;
        Title mTitle;
        mTitle = (Title) findViewById(R.id.title);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.updatepwd_modify_pwd));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UpdateSecurityPasswordActivity.this.finish();
            }
        }, null, null, null);


        View back = findViewById(R.id.rel_back);
        back.setOnClickListener(this);
        editOldPwd = (EditText) this.findViewById(R.id.editText2);
        editNewPwdOne = (EditText) this.findViewById(R.id.edit_new_psd1);
        editNewPwdTwo = (EditText) this.findViewById(R.id.edit_new_psd2);
        editOldPwd.addTextChangedListener(oldPwdWatcher);
        editNewPwdOne.addTextChangedListener(newPwdOneWatcher);
        editNewPwdTwo.addTextChangedListener(newPwdTwoWatcher);

        tvNum = (TextView) this.findViewById(R.id.txt_op_num);
        txt_head_title = (TextView) this.findViewById(R.id.txt_head_title);
        relCommit = (RelativeLayout) this.findViewById(R.id.rel_login);
        relCommit.setOnClickListener(this);
        tvNum.setText(getResources().getString(R.string.update_pwd_secuity));
        txt_head_title.setText(getResources().getString(R.string.update_pwd_secuity));
    }

    private void commit() {
        String old = editOldPwd.getText().toString();
        String new1 = editNewPwdOne.getText().toString();
        String new2 = editNewPwdTwo.getText().toString();
        String oldPsd = mSecurityPasswordSettingsOperation.getSecurityPassword();
        if (old.length() < 6 || new1.length() < 6 || new2.length() < 6) {
            showToast(getResources().getString(R.string.update_pwd_length));
            return;
        }
        if (!old.equals(oldPsd)) {
            showToast(getResources().getString(R.string.update_pwd_error));
            return;
        }
        if (!new1.equals(new2)) {
            showToast(getResources().getString(R.string.update_newpwd_notequal));
            return;
        }
        if (old.equals(new1)) {
            showToast(getResources().getString(R.string.update_pwd_notequal));
            return;
        }
        mSecurityPasswordSettingsOperation.saveSecurityPassword(new1);
        showToast(getResources().getString(R.string.update_pwd_finish));
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rel_back:
                finish();
                break;
            case R.id.rel_login:
                commit();
                break;
        }
    }



    private TextWatcher oldPwdWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            oldPwdLength = s.length();
            checkSubmitBtn();
        }

    };

    private TextWatcher newPwdOneWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            newPwdLengthOne = s.length();
            checkSubmitBtn();
        }

    };

    private TextWatcher newPwdTwoWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }


        @Override
        public void afterTextChanged(Editable s) {
            newPwdLengthTwo = s.length();
            checkSubmitBtn();
        }
    };

    private void checkSubmitBtn() {
        if (newPwdLengthTwo >= 6 && newPwdLengthOne >= 6 && oldPwdLength >= 6) {
            relCommit.setEnabled(true);
            relCommit.setBackgroundResource(R.drawable.login_click_ripple);
            relCommit.setOnClickListener(this);
        } else {
            relCommit.setEnabled(false);
            relCommit.setBackgroundResource(R.drawable.bg_gray_corner_login_btn);
            relCommit.setOnClickListener(null);
        }
    }
}
